package Algorithm.dynamic.palindrome;

/**
 * @author liujun
 * @version 1.0
 * @date 2019-11-07 22:42
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @description 判定回文串
 * 判断给定字符串是否是回文，默认单个字符不够成回文
 */
public class FindPalindromeViolence extends Palindrome {

    //暴力法发现回文
    public String FindPalindrome(String source) {
        if (source == null || source.length() == 0) {
            return null;
        }
        if (source.length() == 1) {
            return source;
        }
        String target = "";
        String pattern = null;
        int length = source.length();
        for (int i = 0; i < length; i++) {
            //因为substring(i,j)截取范围是半开区间[i,j),
            for (int j = i + 2; j <= length; j++) {
                pattern = source.substring(i, j);
                if (JudgePalindrome(pattern) && pattern.length() > target.length()) {
                    target = pattern;
                }
            }
        }
        return target.length() > 0 ? target : null;
    }

}
